﻿using System;
using System.Windows;

namespace MyFloridaHouse.libSvUtilities.Logging
{
    public class Log
    {
        
        public static bool HandleInternalError(string errorMessage)
        {
            if (errorMessage != null)
            {
                if (errorMessage.Length > 0)
                {
                    MessageBox.Show(errorMessage);
                    return true;
                }
            }
            return false;
        }

        public static bool HandleServerError(string methodName, string errorMessage)
        {
            if (errorMessage != null)
            {
                if (errorMessage.Length > 0)
                {
                    MessageBox.Show(methodName + " : " + errorMessage);
                    return true;
                }
            }
            return false;
        }

        public static bool HandleException(Exception ex)
        {
            if (ex == null) return false;
            string message = "";
            bool result = ShowExceptionRecursive(1, ex, ref message);
            if (message.Length > 0)
            {
                MessageBox.Show(message);
            }
            return result;
        }

        public static bool ShowExceptionRecursive(int exceptionLevel, Exception ex, ref string message)
        {
            if (ex == null) return false;

            string dataElements = "";
            foreach (var key in ex.Data)
            {

                dataElements += key.ToString() + Environment.NewLine;
            }

            message += exceptionLevel + " EXCEPTION: " + Environment.NewLine + ex.Message + Environment.NewLine + dataElements + ex.StackTrace;
            ShowExceptionRecursive(exceptionLevel, ex.InnerException, ref message);
            return true;
        }
    }
}
